package com.sgb.kjwl.view.widget;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.PopSelfBinding;
import com.sgb.kjwl.model.entity.user.EssentiaIinformationeEntity;
import com.sgb.kjwl.view.ui.activity.self.SuggestionFeedbackActivity;
import com.sgb.kjwl.view.ui.activity.self.authentication.AuthActivity3;
import com.sgb.link.utils.SizeUtils;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.util.StringUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.DialogFragment;

import org.jetbrains.annotations.NotNull;

public class SelfPopWindow extends DialogFragment implements View.OnClickListener {


    PopSelfBinding binding;
    ClickSelfItemListener clickSelfItemListener;
    EssentiaIinformationeEntity entity;

    int type;

    //创建一个接口
    public interface ClickSelfItemListener {
        void click(String nameStr);
    }

    //设置接口回调
    public void setClickSelfItemListener(ClickSelfItemListener listener) {
        this.clickSelfItemListener = listener;
    }

    public void getPersonalPortrait(EssentiaIinformationeEntity entity,int type) {
        this.entity = entity;
        this.type = type;
    }

    public static SelfPopWindow newInstance() {
        SelfPopWindow fragment = new SelfPopWindow();
        return fragment;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    public void setData(){
        if (entity != null) {
            String realName = entity.getRealName();
            if (!TextUtils.isEmpty(entity.getRealName())) {
                Pattern p = Pattern.compile("[0-9]*");
                Matcher m = p.matcher(realName);
                if (m.matches()) {//姓名正则判断，只含有数字
                    binding.name.setText(StringUtil.getPhoneToName(realName));
                } else {
                    binding.name.setText(TextUtils.isEmpty(realName)?"":realName);
                }
            }
            //设置抽屉布局的默认头像
            if (TextUtils.isEmpty(entity.getImage())) {
                if (0==type) {
                    Glide.with(this)
                            .load(R.mipmap.def_photo)
                            .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                            .into(binding.companyLogoIv);
                } else if (type == 1){
                    Glide.with(this)
                            .load(R.mipmap.shop_img_new_2)
                            .apply(RequestOptions.bitmapTransform(new RoundedCorners(1)))
                            .into(binding.companyLogoIv);
                } else if (type == 2){
                    Glide.with(this)
                            .load(R.drawable.icon_team_default)
                            .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                            .into(binding.companyLogoIv);
                } else {
                    Glide.with(this)
                            .load(R.drawable.icon_organization_default)
                            .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                            .into(binding.companyLogoIv);
                }
            } else {
                //设置抽屉布局里的头像
                if (type == 1){
                    Glide.with(this)
                            .load(entity.getImage() + "?x-oss-process=image/resize,m_fixed,h_150")
                            .apply(RequestOptions.bitmapTransform(new RoundedCorners(1)))
                            .into(binding.companyLogoIv);
                }else {
                    Glide.with(this)
                            .load(entity.getImage() + "?x-oss-process=image/resize,m_fixed,h_150")
                            .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                            .into(binding.companyLogoIv);
                }

            }
            if (type == 2){
                binding.layoutJoin.setVisibility(View.GONE);
            }else {
                binding.layoutJoin.setVisibility(View.VISIBLE);
            }
            if (type==0){
                binding.company.setText("");
                binding.company.setVisibility(View.GONE);
            }else {
                binding.company.setText(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NAME, ""));
                binding.company.setVisibility(View.VISIBLE);
            }
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.pop_self, container, false);
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        binding.layoutPersonInfo.setOnClickListener(this);
        binding.layoutMyCard.setOnClickListener(this);
        binding.layoutJoin.setOnClickListener(this);
        binding.layoutIdentification.setOnClickListener(this);
        binding.layoutCustomerService.setOnClickListener(this);
        binding.layoutShareApp.setOnClickListener(this);
        binding.layoutSetting.setOnClickListener(this);
        binding.layoutFeedback.setOnClickListener(this);
        setData();
        binding.layoutFeedback.setOnClickListener( v -> {
            dismiss();
            SuggestionFeedbackActivity.start(getActivity());
        });

        binding.layoutIdentification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                AuthActivity3.start(getActivity());

            }
        });
        return binding.getRoot();
    }

    @Override
    public void onClick(View v) {
        if (clickSelfItemListener != null) {
            LinearLayout linearLayout = (LinearLayout) v;
            TextView textView = (TextView) linearLayout.getChildAt(1);
            clickSelfItemListener.click(textView.getText().toString());
            dismiss();
        }
    }


//    @Override
//    public void onStart() {
//        super.onStart();
//        Window window = getDialog().getWindow();
//        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//        window.setGravity(Gravity.LEFT | Gravity.BOTTOM);
//        window.setWindowAnimations(R.style.popwindow_left);
//        Dialog dialog = getDialog();
//        if (dialog != null) {
//            DisplayMetrics dm = new DisplayMetrics();
//            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
//            dialog.getWindow().setLayout((int) (SizeUtils.getScreenWidth(getContext()) * 0.79), (int) (SizeUtils.getScreenHeight(getContext()) * 2));
//            //dialog.getWindow().setLayout(dm.widthPixels, dm.heightPixels);
//        }
//    }

    @NonNull
    @NotNull
    @Override
    public Dialog onCreateDialog(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        Window win = dialog.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        win.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparent)));
        lp.gravity = Gravity.LEFT;
        lp.windowAnimations = R.style.popwindow_left;
        lp.width = (int) (SizeUtils.getScreenWidth(getContext()) * 0.79);
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        win.setAttributes(lp);
        return dialog;
    }
}
